﻿#include "rejectview.h"
#include "ui_rejectview.h"

RejectView::RejectView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RejectView)
{
    ui->setupUi(this);

    model=new QSqlQueryModel(this);
    ui->tableView->setModel(model);

    connect(ui->tableView,SIGNAL(clicked(QModelIndex)),this,SLOT(updateDetail(QModelIndex)));
}

RejectView::~RejectView()
{
    delete ui;
}

void RejectView::setHeaderData()
{
    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("ID"));
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("日期"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("联系人"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("快递"));
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("经手"));
    model->setHeaderData(5,Qt::Horizontal,QStringLiteral("备注"));
}

void RejectView::updateDetail(const QModelIndex &index)
{
    if(index.isValid())
    {
        QSqlRecord record=model->record(index.row());
        int id=record.value(0).toInt();

        QSqlQuery query(QString("select content from td_reject where id='%1'").arg(id));
        query.exec();
        if(query.next())
        {
            QString content=query.value(0).toString();
            ui->detail->setPlainText(content);
        }
    }
}

void RejectView::on_btn_full_clicked()
{
    QSqlQuery query("select id,date,contact,express,transactor,remark from td_reject");
    model->setQuery(query);

    setHeaderData();
}

void RejectView::on_btn_undone_clicked()
{
    QSqlQuery query("select id,date,contact,express,transactor,remark from td_reject where done is NULL");
    model->setQuery(query);

    setHeaderData();
}
